package algorithm;

import java.util.ArrayList;

import java.util.Date;
import java.util.List;

import entities.Appointment;
import entities.Calendar;
import entities.Category;
import exceptions.IllegalAppointmentException;
import fachlogik.Administration;

public class MostImportantFirst {

private Administration    admin;
private Calendar          selectedCalendar;
private List<Appointment> allAppointments;
private List<Appointment> mostImportantAppointmentList;
private int               gesamtDauer;
private int               gesamtDauerMiApp;

// Alle Appointments, die rescheduled werden m�ssen
private List<Appointment> rescheduledAppointments;

public MostImportantFirst() {
    admin = Administration.getInstance();
    allAppointments = admin.getCalendarAppointments(selectedCalendar);
    rescheduledAppointments = new ArrayList<>();
    // Liste mit den zu verschiebenden Appointments
    mostImportantAppointmentList = new ArrayList<>();
}

public void mostImportantAppointmentFirst(List<Appointment> liste) {
    while (!liste.isEmpty()) {
        Appointment mostImportant = wichtigstesAppointmentHerausfinden(liste);
        mostImportantAppointmentList.add(mostImportant);
        liste.remove(mostImportant);
    }
}

// bekommt die Liste mit den zu verschiebenden Appointments
public Appointment wichtigstesAppointmentHerausfinden(
        List<Appointment> liste) {
    admin.appointmentsSortieren(liste);
    Appointment mostImportant = null;
    int importance = 0;
    for (Appointment a : liste) {
        int priority = a.getAppointmentPriority();
        if (priority > importance) {
            importance = priority;
            mostImportant = a;
        }
    }
    return mostImportant;
}

public int getMostImportantAppointmentTimespan(Appointment appointment)
{
    Date beginnAppointment = appointment.getBegin();
    Date endeAppointment = appointment.getEnd();
    long a = beginnAppointment.getTime();
    long b = endeAppointment.getTime();
    if (b > a)
    {
        long diff = beginnAppointment.getTime() - beginnAppointment.getTime();
        double diffMinutes = diff / (60 * 1000);
        double diffHours = diffMinutes / 60;
        double diffSeconds = diffMinutes * 60;

        int h = (int) diffHours;
        double x = diffHours - h;
        double y = x * 60;
        int m = (int) y;
        int hInMinuten = h * 60;
        gesamtDauerMiApp = hInMinuten + m;
    }
    else
    {
        System.out.println("");
    }

    return 0;
}

public int getTimespanbetweenAppointments(Appointment appointment1, Appointment appointment2)
{
    Date endeAppointment1 = appointment1.getEnd();
    Date beginnAppointment2 = appointment2.getBegin();
    long a = endeAppointment1.getTime();
    long b = beginnAppointment2.getTime();
    if (b > a)
    {
        long diff = beginnAppointment2.getTime() - endeAppointment1.getTime();
        double diffMinutes = diff / (60 * 1000);
        double diffHours = diffMinutes / 60;
        double diffSeconds = diffMinutes * 60;

        int h = (int) diffHours;
        double x = diffHours - h;
        double y = x * 60;
        int m = (int) y;
        int hInMinuten = h * 60;
        gesamtDauer = hInMinuten + m;
    }
    else
        System.out.println("");

    return 0;
}



public void rescheduleMostIpApp() throws IllegalAppointmentException {
    ArrayList<Appointment> existingApps = (ArrayList<Appointment>)admin.gettimespanOfDatesToReschedule();
    ArrayList<Appointment> appsToReschedule = (ArrayList<Appointment>)admin.getAllrescheduledAppointments();
    
    Date anfangZeitraum = admin.getRescheduleErlaubtVon();
    Date endeZeitraum = admin.getRescheduleErlaubtBis();
    
	for(int i = 0; i < allAppointments.size(); i++)
	{
		if(allAppointments.get(i+1) != null)
		getTimespanbetweenAppointments(allAppointments.get(i), allAppointments.get(i+1));
	
		for(int u = 0; u < mostImportantAppointmentList.size(); i++)
		{
			getMostImportantAppointmentTimespan(mostImportantAppointmentList.get(u));
			Appointment a = mostImportantAppointmentList.get(u);
			Category c = mostImportantAppointmentList.get(u).getCategory();
			if(gesamtDauerMiApp < gesamtDauer)
			{
				if(a.getEnd().before(c.getEndzeit())){
				admin.addOrEditAppointment(mostImportantAppointmentList.get(u));
				mostImportantAppointmentList.remove(u);
				}
			}
		}
	}
}
}

